package com.xscha.soyue.common.core.json;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.Getter;
import lombok.Setter;
import com.fasterxml.jackson.databind.Module;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import java.time.ZoneId;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

/**
 * JSON自定义模块加载
 *
 * @author Vondser
 * @version V2024.1.0
 */
@Getter
@Setter
public class SoyJackson2ObjectMapperBuilderCustomizer implements Jackson2ObjectMapperBuilderCustomizer {

	private List<Module> modules;

	@Override
	public void customize(Jackson2ObjectMapperBuilder builder) {
		if (CollUtil.isNotEmpty(modules)) {
			modules.forEach(module -> {
				if (module instanceof BeanJsonModule) {
					((BeanJsonModule) module).registerModels();
				}
			});
			builder.locale(Locale.CHINA);
			builder.timeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
			builder.simpleDateFormat(DatePattern.NORM_DATETIME_PATTERN);
			builder.serializerByType(Long.class, ToStringSerializer.instance);
			builder.modules(modules);
		}
	}

}
